package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    @Override
    public DtsAdmin findDtsAdminByUsername(String username) {
        QueryWrapper<DtsAdmin> queryWrapper=new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("deleted",false);
        return dtsAdminMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<DtsAdmin> findListByQuery(String username, Integer page, Integer limit, String sort, String order) {
        return dtsAdminMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsAdmin>()
                .like(!StringUtils.isEmpty(username),"username",username)
                .eq("deleted",false)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));


    }

    @Override
    public DtsAdmin create(DtsAdmin dtsAdmin) {
        dtsAdmin.setAddTime(new Date());
        dtsAdmin.setUpdateTime(new Date());

        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));

        dtsAdminMapper.insert(dtsAdmin);
        return dtsAdmin;
    }

    @Override
    public void updatePassword(Integer id,String newPassword) {

        dtsAdminMapper.updatePassword(id,newPassword,new Date());

    }

    @Override
    public void updateAdmin(DtsAdmin dtsAdmin) {

        dtsAdminMapper.updateById(dtsAdmin);
    }

    @Override
    public void deleteAdmin(Integer id, Boolean deleted) {

        dtsAdminMapper.deleteAdmin(id,deleted);

    }

    @Override
    public List<CatVo> findAll() {
        return dtsAdminMapper.findAll();
    }


}

